feat: integrate chat credits handling into chat stream processing#363
Conversation
- Added handleChatCredits function to manage usage tracking for chat sessions. - Updated the chat stream execution to include credit handling after processing messages. - Ensured compatibility with the existing chat completion handling logic.
|
@pradipthaadhi is attempting to deploy a commit to the Recoupable Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughCaptured and retained the streaming result in Changes
Sequence Diagram(s)sequenceDiagram
actor Client
participant Handler as "handleChatStream"
participant Agent
participant UI as "UI Stream"
participant Credits as "handleChatCredits"
participant Billing as "Billing Service"
Client->>Handler: start chat request
Handler->>Agent: agent.stream(chatConfig)
Agent-->>Handler: streamResult
Handler->>UI: streamResult.toUIMessageStream() (merge)
UI-->Client: incremental messages
UI->>Handler: onFinish event
Handler->>Handler: handleChatCompletion(...)
Handler->>Credits: handleChatCredits(usage=await streamResult.usage, model, accountId)
Credits->>Billing: deductCredits(accountId, creditsToDeduct)
Billing-->>Credits: deduction result
Credits-->>Handler: completion
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 1✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
… cost - Modified handleChatCredits to always deduct at least 1 credit, even when usage cost is zero. - Updated related tests to reflect the new behavior, ensuring proper credit deduction is called. - Enhanced test descriptions for clarity on credit deduction logic.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@lib/credits/handleChatCredits.ts`:
- Around line 30-35: The current logic in handleChatCredits.ts always converts
zero-cost events into a 1-credit charge (creditsToDeduct) and unconditionally
calls deductCredits, causing non-billable sessions to consume credits; update
handleChatCredits so you only call deductCredits(accountId, creditsToDeduct)
when an explicit "billable usage happened" condition from the chat/session is
true (derive this from the same signal used in lib/chat/handleChatStream.ts for
real completions), and keep the Math.max(1, Math.round(usageCost * 100)) minimum
calculation only for those billable cases; ensure you still compute
creditsToDeduct the same way but gate the call to deductCredits(...) behind that
billable flag so cached/provider-failed/non-aborted-but-non-billable flows do
not mutate balance or throw.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 56727693-526b-4be3-bf48-8df898f150c3
⛔ Files ignored due to path filters (3)
lib/chat/__tests__/handleChatStream.test.tsis excluded by!**/*.test.*,!**/__tests__/**and included bylib/**lib/chat/__tests__/integration/chatEndToEnd.test.tsis excluded by!**/*.test.*,!**/__tests__/**and included bylib/**lib/credits/__tests__/handleChatCredits.test.tsis excluded by!**/*.test.*,!**/__tests__/**and included bylib/**
📒 Files selected for processing (1)
lib/credits/handleChatCredits.ts
- Added handleChatCredits function to manage usage tracking for chat sessions. - Updated the chat stream execution to include credit handling after processing messages. - Ensured compatibility with the existing chat completion handling logic. - test(api): mock handleChatCredits and DEFAULT_MODEL in handleChatStream tests
…-chat' of https://github.com/pradipthaadhi/api into yuliusupwork/myc-4501-credits-are-not-being-deducted-on-chat
…ucted-on-chat' of https://github.com/pradipthaadhi/api into yuliusupwork/myc-4501-credits-are-not-being-deducted-on-chat" This reverts commit 0589afc, reversing changes made to 5450660.
- Updated handleChatCredits to always deduct at least 1 credit when usage cost is zero. - Modified related tests to verify that credit deduction occurs correctly, even with zero usage cost. - Enhanced test descriptions for clarity on the credit deduction logic.
…-chat' of https://github.com/pradipthaadhi/api into yuliusupwork/myc-4501-credits-are-not-being-deducted-on-chat
Summary by CodeRabbit